package model

import (
	"die4.4/inits"
	"gorm.io/gorm"
)

type User struct {
	gorm.Model
	Name     string `gorm:"type:varchar(20);not null;comment:用户名"`
	Password string `gorm:"type:varchar(32);not null;comment:密码"`
	Email    string `gorm:"type:varchar(20);not null;comment:邮箱"`
}

func (User) TableName() string {
	return "user"
}

func (u *User) Register() error {
	return inits.DB.Create(&u).Error
}

func (u *User) Login(name string, password string) (result User, err error) {
	err = inits.DB.Where("name=? and password=?", name, password).Find(&result).Error
	if err != nil {
		return User{}, err
	}
	return result, nil
}

func (u *User) UserList() (users []User, err error) {
	err = inits.DB.Find(&users).Error
	if err != nil {
		return nil, err
	}
	return users, nil
}
